<template>
  <div>
    <page-dialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-list="dialogInfo.btList"
    >
      <!--左右布局-->
      <!-- 条件栏 -->
      <div class="layui-row">
        <div
          class="SelectProductAddPrice"
          :class="isShowRightTable ? 'layui-col-md6' : ''"
        >
          <NewPageFilter
            v-model:query="filterInfo.query"
            :filter-list="filterInfo.searchList"
            :filter-btn="filterInfo.searchBtn"
            :list-type-info="listTypeInfo"
          />
          <dialog-table
            :refresh="tableInfo.refresh"
            :init-curpage="tableInfo.initCurpage"
            v-model:data="tableInfo.data"
            :api="tableInfo.api"
            :listen-height="false"
            :reserve-selection="true"
            v-model:query="filterInfo.query"
            :field-list="tableInfo.fieldList"
            :handle="tableInfo.handle"
            :highlight-row="highlightRow"
            :check-box="checkBox"
            @handleEvent="handleEvent"
          >
          </dialog-table>
        </div>
        <div
          v-if="isShowRightTable"
          class="layui-col-md6 SelectProductAddPrice SelectProductAddPriceRight"
          style="padding-left: 20px"
        >
          <p style="height: 33px; line-height: 33px" class="slip-edit">
            已选产品
          </p>
          <el-form :model="tableRightInfo" ref="tableForm" style="width: 100%">
            <dialog-table
              v-model:data="tableRightInfo.data"
              :check-box="false"
              :listen-height="false"
              :tab-index="true"
              :field-list="tableRightInfo.fieldList"
              :pager="false"
            >
              <template v-slot:col-capacity="scope">
                <el-input-number
                  controls-position="right"
                  style="width: 100%"
                  v-model="scope.row.capacity"
                  :min="0"
                  :max="99999"
                />
              </template>
            </dialog-table>
          </el-form>
        </div>
      </div>
    </page-dialog>
  </div>
</template>

<script setup name="SelectProduct">
import { getProductPage } from '@/api/core.js'
const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getProductPage,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  showSupplierName: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  showStock: {
    type: Boolean,
    default: false,
  },
  checkedList: {
    type: Array,
    default: () => {
      return []
    },
  },
  isShowRightTable: {
    type: Boolean,
    default: false,
  },
})
const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})
const systemConfig = ref(proxy.$store.state.permission.systemConfig)
// 过滤相关配置
const filterInfo = ref({
  query: {
    searchType: 'product',
    status: 1,
    type: 0,
    sort: 'common_name,standard',
    ...props.query,
  },
  searchList: [
    {
      componentType: 'input',
      label: '名称/别名/编码/助记符',
      value: 'keyword',
      event: search,
      hidden: false,
      show: true,
    },
  ],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: undefined,
  data: [],
  fieldList: [
    { label: '名称', value: 'commonName' },
    { label: '编码', value: 'productCode', minWidth: 80 },
    {
      label: '规格',
      value: 'standard',
      minWidth: 100,
    },
    {
      label: '单位',
      value: 'unitName',
      minWidth: 80,
    },
    {
      label: '厂家',
      value: 'brandName',
    },
    {
      label: '条码类型',
      value: 'codeTypeValue',
    },
    {
      label: '产品分类',
      value: 'productCategoryValue',
      minWidth: 80,
    },
  ],
})
const tableRightInfo = ref({
  data: [],
  fieldList: [
    { label: '名称', value: 'commonName' },
    { label: '编码', value: 'productCode', minWidth: 80 },
    {
      label: '规格',
      value: 'standard',
      minWidth: 100,
    },
    {
      label: '单位',
      value: 'unitName',
      minWidth: 80,
    },
    {
      label: '厂家',
      value: 'brandName',
    },
    {
      label: '条码类型',
      value: 'codeTypeValue',
    },
    {
      label: '产品分类',
      value: 'productCategoryValue',
      minWidth: 80,
    },
    {
      label: '容量',
      value: 'capacity',
      type: 'slot',
    },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: props.isShowRightTable ? '绑定产品' : '添加产品',
  width: '85%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: true,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')

// 获取列表
function getList() {
  tableInfo.value.api = props.api
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.api = props.api
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function changePrice(e, str) {
  proxy.$refs.tableForm.clearValidate(str)
}
async function save() {
  if (tableRightInfo.value.data.length) {
    handleClose()
    emits('callback', tableRightInfo.value.data)
  } else {
    proxy.$message({
      type: 'warning',
      duration: 3500,
      showClose: true,
      message: '请选择产品',
    })
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      if (data.length) {
        tableRightInfo.value.data = data.map((item) => {
          return {
            ...item,
            capacity: 0,
          }
        })
      }
      dialogInfo.value.btList[1].disabled = !data.length
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}

onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    if (props.showStock) {
      tableInfo.value.fieldList.push({
        label: '库存数量',
        value: 'stockQty',
        type: 'column',
        formatter: proxy.$fn.qtyFormatter,
      })
    }
    getList()
  })
})
</script>

<style scoped></style>
